import { observable, action, toJS } from 'mobx';

import { message } from 'antd';
/*
**原action页面的引用
*/
import * as API_LIST from '../apis/votingList';
import objectAssign from 'object-assign';
import moment from 'moment';
import { WeaForm } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { WeaTableNew } from 'comsMobx';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

const URGEN_CONDITION = () => {
  let condition = {
    conditioninfo: [
      {
        defaultshow: true,
        items: [
          {
            colSpan: 2,
            conditionType: 'SELECT',
            domkey: [
              'remindType',
            ],
            fieldcol: 17,
            isQuickSearch: false,
            label: getLabel('501471', '提醒方式'),
            labelID: '501471',
            labelcol: 6,
            precision: 0,
            relatekey: '',
            viewAttr: 3,
            detailtype: 3,
            options: [
              {
                disabled: false,
                key: '0',
                selected: true,
                showname: getLabel('18845', '邮件提醒'),
                visible: true,
              },
              {
                disabled: false,
                key: '1',
                selected: false,
                showname: getLabel('17586', '短信提醒'),
                visible: true,
              },
            ],
            value: '0',
            tipPosition: 'bottom',
          },
          {
            colSpan: 2,
            conditionType: 'TEXTAREA',
            domkey: [
              'content',
            ],
            fieldcol: 17,
            isQuickSearch: false,
            label: getLabel('33368', '内容'),
            labelID: 33368,
            labelcol: 6,
            precision: 0,
            relatekey: '',
            viewAttr: 2,
          },
        ],
      },
    ],
  };
  return condition;
};

export default class VotingList {
  title = '调查结果';
  @observable urgentForm = new WeaForm(); // 催办Form
	@observable userList = [];

	// @observable comsWeaTableStore = new TableStore();
	@observable comsWeaTableStore = {
		basic: new TableStore(),
		dialog: new TableStore(),
	};
	@observable form = {
		basic: new WeaForm(),
	}
	@observable status = {
		loading: false,
		showSearchAd: {
			basic: false,
		},
		allSessionkey: {
			basic: '',
			dialog: '',

		},
		conditions: {
			basic: '',
		},
		rightMenuDatas: [], // 右键菜单数据

		votingDialog: false, // 是否显示弹出框
		votingDialogSpin: false, // 弹出框spin
		votingDialogId: '', // 弹出框id，也就是调查id
		votingDialogList: '', // 弹出框全部数据
		votingDialogTitle: '', // 弹出框标题

		personListDialog: false,
		personListDialogType: '',
		personListSessionkey: '',

		unDoSize: '',
		doSize: '',
		totalSize: '',
		dialogCondition: [],
		WeaRadioGroupParams: {},
		nowParam: {},

		customDialog: false,
		opeatorKeyspan: '',
		customDialogType: '',
  };

  @observable urgentVisible = false; // 催办dialog 显隐
  @observable urgentFileds = []; // 催办表单condition
  @observable urgentLoading = false; // 催办loading
  @observable isurgent = ''; // 是否显示催办btn
  @observable printDetailMap = {}; // 打印详情-查看列表数据

	constructor() {

  }

  @action
  setStoreState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

	@action
	// 获得详细数据
  getData = (params = { votingid: this.status.votingDialogId }) => {
		API_LIST.getData(params).then((result) => {
		});
	}

	addUser(params) {
		this.userList.push({ name: 'dyf' });
	}

	getUserList(params) {
		return this.userList.slice();
	}


	doLoading = (loading) => {
		this.status = { ...this.status, loading };
	}

	doInit = (callback) => {
		let p1 = API_LIST.result({});
		let p2 = API_LIST.condition({ listType: 'result' });
		let p3 = API_LIST.rightMenu({ listType: 'result' });

		Promise.all([p1, p2, p3]).then((result) => {
			this.comsWeaTableStore.basic.getDatas(result[0].sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, basic: result[0].sessionkey };
			let conditions = { ...this.status.conditions, basic: result[1].condition };

			this.status = {
				...this.status,
				allSessionkey,
				conditions,
        rightMenuDatas: result[2].rightMenus,
			};
			if (callback) callback(result[1].condition);
		});
	}


	doSearch = (param) => {
		this.status = {
			...this.status,
			loading: true,
		};
		API_LIST.result({ ...param }).then((result) => {
			this.comsWeaTableStore.basic.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, basic: result.sessionkey };
			this.status = {
				...this.status,
				allSessionkey,
				loading: false,
			};
			this.setShowSearchAd(false, 'basic');
		});
	}
	setShowSearchAd = (bool, type = 'basic') => {
		let showSearchAd = { ...this.status.showSearchAd, [type]: bool };
		this.status = { ...this.status, showSearchAd };
	}

	setVotingDialog = (bool, id = '', title) => { // 关闭的时候重置数据
		this.status = {
			...this.status,
			votingDialog: bool,
			votingDialogSpin: bool,
			votingDialogId: id,
			votingDialogTitle: title,
			unDoSize: '',
			doSize: '',
			totalSize: '',
			dialogCondition: [],
		};
		bool && this.getDialogAllDatas(id, {});
  }
  // 获取统计范围列表
	getRadioGroup = (isEdit = false, params = {}) => {
    let res = isEdit ? API_LIST.viewConditionEdit(params) : API_LIST.viewCondition();
    res.then(((result) => {
      this.status = {
        ...this.status,
        dialogCondition: result.condition[0].items,
      };
    }));
  }
	getDialogAllDatas = async (id = this.status.votingDialogId, params) => {
	  if (!id) return null; // 关闭窗口时不调用方法内容
 		this.status = {
			...this.status,
			votingDialogSpin: true,
			WeaRadioGroupParams: params,
		};
		let p1 = API_LIST.viewResult({ votingid: id, ...params });
		let p2 = API_LIST.partSituation({ votingid: id, ...params });
		let res = await Promise.all([p1, p2]).then((result) => {
      this.isurgent = result[0].votingStatus;
			this.status = {
				...this.status,
				votingDialogSpin: false,
				votingDialogList: result[0].datas,
				unDoSize: result[1].unDoSize,
				doSize: result[1].doSize,
				totalSize: result[1].totalSize,
				// dialogCondition: result[1].condition[0].items,
			};
    });
    return res;
  }

  getPrintPreview = (id, params, callback) => {
    this.getRadioGroup(true, { ...params, votingid: id }); // ***
    this.getDialogAllDatas(id, params).then(() => {
      if (callback && typeof callback === 'function') callback();
    });
  }

	setPersonListDialog = (bool, type, questionid = '', optionid = '', notShowExport = false) => {
		let personListDialogType = type == 'participate' ? getLabel(385158, '已参与人员名单') :
			type == 'notInvolved' || type == 'urgent' ? getLabel(385159, '未参与人员名单') :
				type == 'other' ? getLabel(25740, '其他') :
					type == 'agree' ? getLabel(385160, '意见或建议') :
						type == 'questionList' || type == 'optionList' ? getLabel(385161, '投票人员清单') : '';

		this.status = {
			...this.status,
			personListDialog: bool,
			type,
      personListDialogType,
      notShowExport,
		};
		let param = { ...this.status.WeaRadioGroupParams, questionid, optionid };
		this.status = {// 把搜索的参数存起来，后面其它搜索用
			...this.status,
			nowParam: param,
		};
		type != '' && this.getPersonList(type, param);
	}
	getPersonList = (type, param = this.status.nowParam, searchParam = {}) => {
		let params = {
			...param,
			votingid: this.status.votingDialogId,
			...searchParam,

		};
		type = this.status.type;
		type == 'notInvolved' && API_LIST.undoPerson(params).then((result) => {
			this.comsWeaTableStore.dialog.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, dialog: result.sessionkey };

			this.status = {
				...this.status,
				allSessionkey,
			};
		});
		type == 'urgent' && API_LIST.undoPerson({ votingid: this.status.votingDialogId, ...searchParam }).then((result) => {
			this.comsWeaTableStore.dialog.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, dialog: result.sessionkey };

			this.status = {
				...this.status,
				allSessionkey,
			};
		});
		type == 'participate' && API_LIST.doPerson(params).then((result) => {
			this.comsWeaTableStore.dialog.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, dialog: result.sessionkey };

			this.status = {
				...this.status,
				allSessionkey,
			};
		});
		type == 'other' && API_LIST.doPersonOfOther(params).then((result) => {
			this.comsWeaTableStore.dialog.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, dialog: result.sessionkey };

			this.status = {
				...this.status,
				allSessionkey,
			};
		});
		type == 'agree' && API_LIST.doPersonOfInput(params).then((result) => {
			this.comsWeaTableStore.dialog.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, dialog: result.sessionkey };

			this.status = {
				...this.status,
				allSessionkey,
			};
		});


		type == 'questionList' && API_LIST.doPersonOfQuesion(params).then((result) => {
			this.comsWeaTableStore.dialog.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, dialog: result.sessionkey };

			this.status = {
				...this.status,
				allSessionkey,
			};
		});
		type == 'optionList' && API_LIST.doPersonOfOption(params).then((result) => {
			this.comsWeaTableStore.dialog.getDatas(result.sessionkey, 1);
			let allSessionkey = { ...this.status.allSessionkey, dialog: result.sessionkey };

			this.status = {
				...this.status,
				allSessionkey,
			};
		});
	}

	setCustomDialog = (bool, opeatorKeyspan, type) => {
		this.status = {
			...this.status,
			customDialog: bool,
			opeatorKeyspan,
			customDialogType: type,
		};
  }

  /* **************************************************** 调查催办 *************************************************** */
  @action getUrgenCondition = (callback) => {
    this.urgentLoading = true;
    let urgentCondition = URGEN_CONDITION();
    this.urgentForm.initFormFields(urgentCondition.conditioninfo);
    this.urgentFileds = urgentCondition.conditioninfo;
    this.urgentLoading = false;
    if (callback) callback();
  }

  @action submitUrgen = (callback) => {
    let formParams = this.urgentForm.getFormParams();
    let params = { ...formParams, votingid: this.status.votingDialogId };
    API_LIST.urgent(params).then((res) => {
      if (res.status === '1') {
        message.success(getLabel('385379', '催办成功！'));
      } else {
        message.error(getLabel('384402', '操作失败，请重试！'));
      }
      if (callback) callback();
    });
  }

  /* ******************************************************************************************************* */
}
